<?php

namespace App\Http\Controllers\Admin;
use Hash;
use DB;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserFormController;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        // $users=DB::table('user')->paginate(10);
        //每页显示条数
        $num=$request->input('num',10);
    //检测是否提交查询关键字
        if($request->has('keywords')){
            $users=DB::table('user')->where('username','like','%'.$request->input('keywords').'%')->paginate($num);
        }else{
            $users=DB::table('user')->paginate($num);
        }

        $data=$request->except('page');

        return view('Admin.user.index',['users'=>$users]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getAdd()
    {
        return view('Admin.user.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postInsert(UserFormController $request)
    {
        $data=$request->except('_token');
        $data['pic']=$this->upload($request);
        $data['password']=Hash::make($data['password']);
        $res=DB::table('user')->insertGetId($data);
        if($res){
            return redirect('admin/user/index')->with('success','用户添加成功');
        }else{
            return back()->with('error','用户添加失败');
        }

    }

    public function upload($request)
    {   //检测是否有文件上传
        if($request->hasFile('pic')){
            $name=md5(time()+rand(1,9999));
            //后缀名
            $s=$request->file('pic')->getClientOriginalExtension();
            $request->file('pic')->move('./public/uploads',$name.'.'.$s);
            return '/public/uploads/'.$name.'.'.$s;
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postDel(Request $request)

    {$id=$request->input('id');
         $res=DB::table('user')->where('id',$id)->delete();
         echo $res;
         
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getEdit($id)
    {
        $users=DB::table('user')->where('id',$id)->first();
        return view('Admin.user.edit',['users'=>$users]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request)
    {
        $data=$request->except('_token');

        if($request->hasFile('pic')){
            $data['pic']=$this->upload($request);
        }
        $res=DB::table('user')->where('id',$data['id'])->update($data);
        if($res){
            return redirect('admin/user/index')->with('success','用户修改成功');
        }else{
            return back()->with('error','用户修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
